﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace DoAn
{
    class Introduction : GameEntity
    {
        Video _video;
        VideoPlayer _player;
        Texture2D _videoTexture;

        public Introduction(ContentManager content, string video)
        {
            _video = content.Load<Video>(video);
            _player = new VideoPlayer();
        }

        public void Update(GameTime gameTime)
        {
            if (_player.State == MediaState.Stopped)
            {
                _player.IsLooped = true;
                _player.Play(_video);
            }
            //Pad
            if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
                _player.Resume();

            if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
                _player.Pause();

            //Keyboard
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter))
                _player.Resume();
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Space))
                _player.Pause();
        }

        public void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch, GameTime gameTime)
        {
            // Only call GetTexture if a video is playing or paused
            if (_player.State != MediaState.Stopped)
                _videoTexture = _player.GetTexture();

            // Drawing to the rectangle will stretch the 
            // video to fill the screen
            Rectangle screen = new Rectangle(graphics.Viewport.X,
                graphics.Viewport.Y,
                graphics.Viewport.Width,
                graphics.Viewport.Height);

            // Draw the video, if we have a texture to draw.
            if (_videoTexture != null)
                spriteBatch.Draw(_videoTexture, screen, Color.White);
        }

        public void Stop()
        {
            _player.Stop();
        }
    }
}